import 'package:flutter/material.dart';
import '../../core/elegance_theme.dart';

/// 头像组件形状枚举
enum EAvatarShape {
  /// 圆形头像
  circle,
  
  /// 方形头像
  square,
}

/// 头像组件大小枚举
enum EAvatarSize {
  /// 小型头像
  small,
  
  /// 普通头像
  normal,
  
  /// 大型头像
  large,
}

/// 头像组件
class EAvatar extends StatelessWidget {
  /// 创建EAvatar组件
  const EAvatar({
    super.key,
    this.src,
    this.icon,
    this.text,
    this.size = EAvatarSize.normal,
    this.shape = EAvatarShape.circle,
    this.backgroundColor,
    this.textColor,
    this.borderColor,
    this.borderWidth = 0,
  });

  /// 头像图片路径
  final String? src;
  
  /// 头像图标
  final IconData? icon;
  
  /// 头像文本
  final String? text;
  
  /// 头像大小
  final EAvatarSize size;
  
  /// 头像形状
  final EAvatarShape shape;
  
  /// 头像背景色
  final Color? backgroundColor;
  
  /// 文本颜色
  final Color? textColor;
  
  /// 边框颜色
  final Color? borderColor;
  
  /// 边框宽度
  final double borderWidth;
  
  /// 获取头像尺寸
  double _getAvatarSize() {
    switch (size) {
      case EAvatarSize.small:
        return 28.0;
      case EAvatarSize.large:
        return 48.0;
      case EAvatarSize.normal:
        return 36.0;
    }
  }
  
  /// 获取文本字体大小
  double _getTextSize() {
    switch (size) {
      case EAvatarSize.small:
        return 12.0;
      case EAvatarSize.large:
        return 20.0;
      case EAvatarSize.normal:
        return 16.0;
    }
  }
  
  /// 获取边框圆角
  double _getBorderRadius() {
    if (shape == EAvatarShape.circle) {
      return _getAvatarSize() / 2;
    }
    return 4.0;
  }
  
  @override
  Widget build(BuildContext context) {
    final theme = EleganceTheme.of(context);
    final avatarSize = _getAvatarSize();
    final textSize = _getTextSize();
    final borderRadius = _getBorderRadius();
    
    // 确定背景色
    final bgColor = backgroundColor ?? theme.primaryColor;
    
    // 确定文本颜色
    final color = textColor ?? Colors.white;
    
    // 构建头像内容
    Widget content;
    if (src != null && src!.isNotEmpty) {
      // 图片头像
      content = ClipRRect(
        borderRadius: BorderRadius.circular(borderRadius),
        child: Image.network(
          src!,
          width: avatarSize,
          height: avatarSize,
          fit: BoxFit.cover,
          errorBuilder: (context, error, stackTrace) {
            // 图片加载失败时显示文本或图标
            return _buildFallbackContent(bgColor, color, textSize, avatarSize);
          },
        ),
      );
    } else {
      // 文本或图标头像
      content = Container(
        width: avatarSize,
        height: avatarSize,
        decoration: BoxDecoration(
          color: bgColor,
          borderRadius: BorderRadius.circular(borderRadius),
        ),
        alignment: Alignment.center,
        child: _buildFallbackContent(bgColor, color, textSize, avatarSize),
      );
    }
    
    // 添加边框
    if (borderWidth > 0) {
      content = Container(
        width: avatarSize + borderWidth * 2,
        height: avatarSize + borderWidth * 2,
        decoration: BoxDecoration(
          color: borderColor ?? Colors.white,
          borderRadius: BorderRadius.circular(borderRadius + borderWidth),
        ),
        alignment: Alignment.center,
        child: content,
      );
    }
    
    return content;
  }
  
  /// 构建备用内容（文本或图标）
  Widget _buildFallbackContent(Color bgColor, Color color, double textSize, double avatarSize) {
    if (icon != null) {
      // 图标头像
      return Icon(
        icon,
        size: avatarSize * 0.6,
        color: color,
      );
    } else if (text != null && text!.isNotEmpty) {
      // 文本头像
      return Text(
        text!.substring(0, text!.length > 2 ? 2 : text!.length),
        style: TextStyle(
          color: color,
          fontSize: textSize,
          fontWeight: FontWeight.bold,
        ),
      );
    }
    
    // 默认头像
    return Container();
  }
}